package Dao;

import java.awt.print.Printable;

import javax.annotation.Resource;

import org.junit.Test;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSON;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import wendy.watson.mapping.Euro;
import wendy.watson.mapping.User;
import wendy.watson.mapping.Vs;

@Repository(value = "scheduleDao")
public class scheduleDaoImp implements ScheduleDao {

	@Resource
	private SqlSessionTemplate sqlSessionTemplate;

	public SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSessionTemplate;
	}

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}

	public scheduleDaoImp() {
		// TODO Auto-generated constructor stub
	}

	public void Printable() {
		System.out.print(sqlSessionTemplate);
	}

	@Override
	public HashMap<String, Object> createschedule() {
		// TODO Auto-generated method stub
		int x;
		String statement = "wendy.watson.mapping.userMapper.teamlist";
		String statement0 = "wendy.watson.mapping.userMapper.teamnumber";
		String statement1 = "wendy.watson.mapping.userMapper.vs";
		String statement2 = "wendy.watson.mapping.userMapper.destroying";

		sqlSessionTemplate.select(statement2, null);

		List<User> users = sqlSessionTemplate.selectList(statement, null);
		int num = sqlSessionTemplate.selectOne(statement0, null);

		String[] team = new String[num];
		System.out.println("球队的数量：" + num);

		int flag = 0;
		for (User user : users) {

			System.out.println("球队名称： " + user.getTeam());
			if (flag < num) {
				team[flag] = user.getTeam();
			}
			flag++;

		}

		int size = team.length - 1;
		// 如果球队数量大于2且为单数需要一个比原来存储球队名的数组多一个空间的扩展数组.该扩展数组储存所有原数组中球队名且末尾为null
		String[] exteam = new String[size + 2];
		if ((size + 1) % 2 == 1) {
			for (int e = 0; e < team.length; e++) {
				exteam[e] = team[e];
			}
			System.out.println("扩展球队表：" + Arrays.toString(exteam));

			String tail = exteam[exteam.length - 1];

			for (int vs = 1; vs <= team.length - 1; vs++) {
				// 只需要将数组最后一个元素保存，在数组第二个元素到倒数第二个元素通过赋值的方式整体向后移动一位后，再把第二
				// 位数组元素赋予原先最末尾的值
				tail = exteam[exteam.length - 1];
				for (int i = exteam.length - 1; i > 1; i--) {

					exteam[i] = exteam[i - 1];
					if (i == 2) {

						exteam[1] = tail;
					}

				}

				// 每轮相隔1周时间
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
				long week = (vs - 2) * 1000 * 60 * 60 * 24 * 7;
				Date date = new Date(System.currentTimeMillis() + week);// （获取启动时间）产生一个当前的毫秒，这个毫秒其实就是自1970年1月1日0时起的毫秒数
				System.out.println(df.format(date));// new Date()为获取当前系统时间
				String fromDate = df.format(date);// 将当前（启动）时间转换为String
				System.out.println(fromDate);

				System.out
						.println("第轮" + vs + "排序表：" + Arrays.toString(exteam));
				// 如果球队遭遇轮空，输出轮空信息
				for (int s = 0, t = exteam.length - 1; s <= t; s++, t--) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("teamleft", exteam[s] + "");
					map.put("teamright", exteam[t] + "");
					map.put("round", vs);
					map.put("time", fromDate);
					x = sqlSessionTemplate.insert(statement1, map);
					if (exteam[s] == null) {
						System.out.println(exteam[t] + " draw a bye. ");
					} else if (exteam[t] == null) {
						System.out.println(exteam[s] + " draw a bye. ");
					} else {
						System.out.println(exteam[s] + " vs " + exteam[t]);
					}
				}

			}

		} else {

			String tail = team[size];
			System.out.println("原球队表" + Arrays.toString(team));
			for (int vs = 1; vs <= team.length - 1; vs++) {
				// 只需要将数组最后一个元素保存，在数组第二个元素到倒数第二个元素通过赋值的方式整体向后移动一位后，再把第二
				// 位数组元素赋予原先最末尾的值
				tail = team[size];
				for (int i = team.length - 1; i > 1; i--) {

					team[i] = team[i - 1];
					if (i == 2) {

						team[1] = tail;
					}

				}
				// 每轮相隔1周时间
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
				long week = (vs - 2) * 1000 * 60 * 60 * 24 * 7;
				Date date = new Date(System.currentTimeMillis() + week);// （获取启动时间）产生一个当前的毫秒，这个毫秒其实就是自1970年1月1日0时起的毫秒数
				System.out.println(df.format(date));// new Date()为获取当前系统时间
				String fromDate = df.format(date);// 将当前（启动）时间转换为String
				System.out.println(fromDate);
				System.out.println("第轮" + vs + "排序表：" + Arrays.toString(team));
				for (int s = 0, t = team.length - 1; s <= t; s++, t--) {
					System.out.println(team[s] + " vs " + team[t]);
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("teamleft", team[s]);
					map.put("teamright", team[t]);
					map.put("round", vs);
					map.put("time", fromDate);
					x = sqlSessionTemplate.insert(statement1, map);
				}

			}
		}

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("teamnumber", num);
		map.put("teamlist", users);

		return map;

	}

	@Override
	public String show(HashMap<String, Object> map) {
		// TODO Auto-generated method stub

		String statement1 = "wendy.watson.mapping.userMapper.filterlist";
		String statement2 = "wendy.watson.mapping.userMapper.count_filter";

		int RowCount = sqlSessionTemplate.selectOne(statement2, map);
		System.out.println("RowCount " + RowCount);
		List<Vs> listUsers0 = sqlSessionTemplate.selectList(statement1, map);
		ArrayList<Object> list0 = new ArrayList<Object>();
		for (Vs a : listUsers0) {
			HashMap<String, String> map0 = new HashMap<String, String>();
			map0.put("id", a.getId() + "");
			map0.put("round", a.getRound() + "");
			map0.put("teamleft", a.getTeamleft() + "");
			map0.put("teamright", a.getTeamright() + "");
			map0.put("time", a.getTime() + "");

			String jsonString0 = JSON.toJSONString(map0);
			list0.add(jsonString0);
		}

		String list = "{\"total\" : " + RowCount + ", \"rows\" : " + list0
				+ "}";
		System.out.println("获得的json" + list);
		return list;
	}

	@Override
	public String union(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		String statement = "wendy.watson.mapping.userMapper.union";
		String statement2 = "wendy.watson.mapping.userMapper.union2";

		int RowCount = sqlSessionTemplate.selectOne(statement2, map);

		List<Vs> listUsers0 = sqlSessionTemplate.selectList(statement, map);

		System.out.println("RowCount " + RowCount);
		ArrayList<Object> list0 = new ArrayList<Object>();
		for (Vs a : listUsers0) {
			HashMap<String, String> map0 = new HashMap<String, String>();
			map0.put("id", a.getId() + "");
			map0.put("round", a.getRound() + "");
			map0.put("teamleft", a.getTeamleft() + "");
			map0.put("teamright", a.getTeamright() + "");
			map0.put("time", a.getTime() + "");

			String jsonString0 = JSON.toJSONString(map0);
			list0.add(jsonString0);
		}

		String list = "{\"total\" : " + RowCount + ", \"rows\" : " + list0
				+ "}";
		System.out.println("获得的json" + list);
		return list;
	}

	@Override
	public String euro() {
		// TODO Auto-generated method stub
		String statement = "wendy.watson.mapping.userMapper.listall";
		List<Euro> listUsers0 = sqlSessionTemplate.selectList(statement, null);
		ArrayList<Object> list0 = new ArrayList<Object>();
		for (Euro a : listUsers0) {
			HashMap<String, String> map0 = new HashMap<String, String>();
			map0.put("id", a.getId() + "");
			map0.put("team", a.getTeam() + "");
			map0.put("number", a.getNumber() + "");

			String jsonString0 = JSON.toJSONString(map0);
			list0.add(jsonString0);
		}

		String list = list0 + "";
		System.out.println("获得的json" + list);
		return list;
	}

	@Override
	public String vsbyone(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		String statement1 = "wendy.watson.mapping.userMapper.union3";

		List<Vs> listUsers0 = sqlSessionTemplate.selectList(statement1, map);
		int RowCount = listUsers0.size();
		System.out.println("RowCount " + RowCount);
		ArrayList<Object> list0 = new ArrayList<Object>();
		for (Vs a : listUsers0) {
			HashMap<String, String> map0 = new HashMap<String, String>();
			map0.put("id", a.getId() + "");
			map0.put("round", a.getRound() + "");
			map0.put("teamleft", a.getTeamleft() + "");
			map0.put("teamright", a.getTeamright() + "");
			map0.put("time", a.getTime() + "");

			String jsonString0 = JSON.toJSONString(map0);
			list0.add(jsonString0);
		}

		String list = "{\"total\" : " + RowCount + ", \"rows\" : " + list0
				+ "}";
		System.out.println("获得的json" + list);
		return list;
	}
    
	//获得某轮赛程表
	@Override
	public String round(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		String statement1 = "wendy.watson.mapping.userMapper.round";

		List<Vs> listUsers0 = sqlSessionTemplate.selectList(statement1, map);
		int RowCount = listUsers0.size();
		System.out.println("RowCount " + RowCount);
		ArrayList<Object> list0 = new ArrayList<Object>();
		for (Vs a : listUsers0) {
			HashMap<String, String> map0 = new HashMap<String, String>();
			map0.put("id", a.getId() + "");
			map0.put("round", a.getRound() + "");
			map0.put("teamleft", a.getTeamleft() + "");
			map0.put("teamright", a.getTeamright() + "");
			map0.put("time", a.getTime() + "");

			String jsonString0 = JSON.toJSONString(map0);
			list0.add(jsonString0);
		}

		String list = "{\"total\" : " + RowCount + ", \"rows\" : " + list0
				+ "}";
		System.out.println("获得的json" + list);
		return list;
	}
    
	//获得轮次数
	@Override
	public String ground() {
		// TODO Auto-generated method stub
		String statement = "wendy.watson.mapping.userMapper.groupround";
		List<Vs> listUsers0 = sqlSessionTemplate.selectList(statement, null);
		ArrayList<Object> list0 = new ArrayList<Object>();
		for (Vs a : listUsers0) {
			HashMap<String, String> map0 = new HashMap<String, String>();
			map0.put("id", a.getId() + "");
			map0.put("round", a.getRound() + "");
			map0.put("teamleft", a.getTeamleft() + "");
			map0.put("teamright", a.getTeamright() + "");
			map0.put("time", a.getTime() + "");

			String jsonString0 = JSON.toJSONString(map0);
			list0.add(jsonString0);
		}

		String list = list0 + "";
		System.out.println("获得的json" + list);
		return list;
	}
}
